package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/messages")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 根据环信用户id,查询用户详情
     */
    @GetMapping("/userinfo")
    public ResponseEntity userinfo(String huanxinId){
        UserInfoVo vo = messageService.findUserInfoByHuanxin(huanxinId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 添加好友
     */
    @PostMapping("/contacts")
    public ResponseEntity contacts(@RequestBody Map map){
        //前端传递过来的用户id是String类型
        Long fiendId = Long.valueOf(map.get("userId").toString());
        messageService.contacts(fiendId);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询联系人列表
     */
    @GetMapping("/contacts")
    public ResponseEntity contacts( @RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize,
                                    String keyword){
        PageResult pr = messageService.findFriends(page,pagesize,keyword);
        return ResponseEntity.ok(pr);
    }

    /**
     * 点赞列表（分页查询）
     */
    @GetMapping("/likes")
    public ResponseEntity likes( @RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        Integer commentType = 1;
        PageResult pr = messageService.comments(page,pagesize,commentType);
        return ResponseEntity.ok(pr);
    }

    /**
     * 评论列表（分页查询）
     */
    @GetMapping("/comments")
    public ResponseEntity comments( @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pagesize){
        Integer commentType = 2;
        PageResult pr = messageService.comments(page,pagesize,commentType);
        return ResponseEntity.ok(pr);
    }

    /**
     * 喜欢列表（分页查询）
     */
    @GetMapping("/loves")
    public ResponseEntity loves( @RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        Integer commentType = 3;
        PageResult pr = messageService.comments(page,pagesize,commentType);
        return ResponseEntity.ok(pr);
    }

    /**
     * 公共列表（分页查询）
     */
    @GetMapping("/announcements")
    public ResponseEntity announcements( @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr = messageService.announcements(page,pagesize);
        return ResponseEntity.ok(pr);
    }
}
